
package com.lf2.tool;

public class CharUtil
{
    public CharUtil() {}
    
    public static char[] fill(char[] srcBytes, char b)
    {
    	int i;
    	for(i=0; i<srcBytes.length; i++)
    	{
    		srcBytes[i] = b;
    	}
        return srcBytes;
    }
    
    public static char[] clearblankByte(char[] srcBytes)
    {
    	char[] bytes;
    	int i;
    	for(i=0; i<srcBytes.length; i++)
    	{
    		if(srcBytes[i] == 0x00)
    		{
    			break;
    		}
    	}
    	bytes = new char[i];
    	System.arraycopy(srcBytes, 0, bytes, 0, i);
    	return bytes;
    }
    
    public static byte[] charToByte(char c)
    {
        byte[] b = new byte[2];
        b[0] = ((byte)((c & 0xFF00) >> 8));
        b[1] = ((byte)(c & 0xFF));
        return b;
    }

    public static char byteToChar(byte[] b) 
    {
        char c = (char)((b[0] & 0xFF) << 8 | b[1] & 0xFF);
        return c;
    }

    public static byte[] getBytes(char[] chars)
    {
        byte[] bytes = new byte[chars.length];
        for (int i = 0; i < bytes.length; i++) {
            bytes[i] = ((byte)(chars[i] & 0xFF));
        }
        return bytes;
    }
    
    public static byte[] getBytes(char[] chars, int offset)
    {
        byte[] bytes = new byte[chars.length-offset];
        for (int i = 0; i < bytes.length; i++) {
            bytes[i] = ((byte)(chars[i+offset] & 0xFF));
        }
        return bytes;
    }
    
    public static byte[] getBytes(char chars)
    {
        byte[] bytes = new byte[1];
        for (int i = 0; i < 1; i++) {
            bytes[i] = ((byte)(chars & 0xFF));
        }
        return bytes;
    }
    
    public static String getStrings(char[] chars)
    {
        byte[] bytes = new byte[chars.length];
        for (int i = 0; i < chars.length; i++) {
            bytes[i] = ((byte)(chars[i] & 0xFF));
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < bytes.length; i++) {
            sb.append(String.format("%02X",  bytes[i]));
        }
        return sb.toString();
    }
    
    public static String getStrings(char chars)
    {
        byte[] bytes = new byte[1];
        for (int i = 0; i < 1; i++) {
            bytes[i] = ((byte)(chars & 0xFF));
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < bytes.length; i++) {
            sb.append(String.format("%02X",  bytes[i]));
        }
        return sb.toString();
    }
    
    public static String getStrings(char chars, int mode)
    {
        byte[] bytes = new byte[1];
        for (int i = 0; i < 1; i++) {
            bytes[i] = ((byte)(chars & 0xFF));
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < bytes.length; i++) {
        	if(mode == 0)
        	{
        		sb.append(String.format("%X",  bytes[i]));
        	}
        	else
        	{
        		sb.append(String.format("%02X",  bytes[i]));
        	}
        }
        return sb.toString();
    }
    
    public static char[] getChars(byte[] bytes)
    {
        char[] chars = new char[bytes.length];
        for (int i = 0; i < chars.length; i++) {
            chars[i] = ((char)(0xFF & bytes[i]));
        }
        return chars;
    }
    
    public static char[] getChars(byte bytes)
    {
        char[] chars = new char[1];
        for (int i = 0; i < 1; i++) {
            chars[i] = ((char)(0xFF & bytes));
        }
        return chars;
    }
    
    public static char[] getChars(char[] chars, int offset)
    {
    	char[] bytes = new char[chars.length-offset];
        for (int i = 0; i < bytes.length; i++) {
            bytes[i] = chars[i+offset];
        }
        return bytes;
    }
}
